package org.jackysoft.service.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.data.AbstractDataProvider;
import org.jackysoft.entity.OfficeDocument;
import org.jackysoft.mapper.OfficeDocumentMapper;
import org.jackysoft.service.OfficeDocumentService;
import org.jackysoft.util.FileDownload;
import org.jackysoft.util.Office2PDF;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.zhuozhengsoft.pageoffice.FileSaver;

@Service
public class OfficeDocumentServiceImpl extends
		AbstractDataProvider<OfficeDocument> implements OfficeDocumentService {

	static final Log logger = LogFactory
			.getLog(OfficeDocumentServiceImpl.class);
	@Resource
	OfficeDocumentMapper mapper;

	public OfficeDocumentServiceImpl() {
		super();
	}

	/**
	 * 调用系统打印机把word转换成PDF
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 * */
	@Override
	public String printToPDF(String docid) throws InterruptedException,
			ExecutionException {
		String pdfDir = context.pdfPath;
		OfficeDocument office = mapper.findOne(docid);
		if (office == null) {
			logger.error("id of document office document can not be found in some path");
			return "error";
		}
		String name = "";
		try {
			InputStream input = new BufferedInputStream(new FileInputStream(
					office.resolverRealPath(context.OFFICEDocBase)));

			ExecutorService executor = Executors.newSingleThreadExecutor();
			Office2PDF wpf = new Office2PDF(context.pdfServer,
					context.pdfServerPort, office.getExt(), input);
			Future<String> future = executor.submit(wpf);
			name = future.get();
			executor.shutdown();
		} catch (IOException e) {
			logger.error(e);
		}
		return pdfDir + File.separator + name + ".pdf";
	}

	@Override
	public OfficeDocument findByRefid(long refid) {
		OfficeDocument doc = mapper.findByRefid(refid);
		return doc;
	}

	@Override
	public FileDownload downloadByRefid(long refid) {
		OfficeDocument doc = this.findByRefid(refid);
		String fname = String.format("%s%s%s", context.OFFICEDocBase,
				File.separator, doc.getPath());
		FileDownload fd = new FileDownload();

		try {
			fd.setFileType(doc.getExt());
			fd.setTitle(doc.getName());
			fd.setInputStream(new BufferedInputStream(
					new FileInputStream(fname)));
		} catch (FileNotFoundException e) {
			logger.error(e);
		}
		return fd;
	}

	@Override
	public void saveDoc(long refid, FileSaver fs) {
		OfficeDocument doc = mapper.findByRefid(refid);
		boolean fresh = doc == null;
		if (fresh)
			doc = new OfficeDocument();
		saveMyDoc(refid, fs, doc, fresh);
	}

	void saveMyDoc(long refid, FileSaver fs, OfficeDocument doc, boolean fresh) {
		doc.setPath(String.format("%s%s.%s", File.separator, doc.getId(),
				fs.getFileExtName()));
		doc.setCreateDate(DateTime.now().getMillis());
		doc.setExt(fs.getFileExtName());
		doc.setRefid(refid);
		fs.saveToFile(String.format("%s%s%s.%s", context.OFFICEDocBase,
				File.separator, doc.getId(), fs.getFileExtName()));
		if (fresh)
			mapper.inert(doc);
		else
			mapper.update(doc);

	}

}